<?php

namespace App\Http\Controllers\SystemManagement;

use App\Models\Constant\Fields;
use App\Models\SystemManagement\Banci;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * Class BanCiController
 * @package App\Http\Controllers\SystemManagement
 * @group 班次管理
 */
class BanCiController extends Controller
{
    use SoftDeletes;
    /**
     * 班次列表
     *
     * @return \Illuminate\Http\Response
     *
     * @queryParam id 班次id
     * @queryParam name 班次名称
     * @queryParam number 上下班次数
     * @queryParam work_time 工作时间
     * @queryParam clock 是否打卡 0:打卡 1:不打卡
     * @queryParam allowedLate 允许迟到时间
     * @queryParam seriousLate 严重迟到时间
     * @queryParam absenteeism 旷工迟到时间
     *
     * @response 200 {"msg":"success","code":200,"data":[{"id":1,"name":"0\u73ed","work_time":"0:00-6:00"},{"id":2,"name":"\u4e00\u73ed","work_time":"7:00-9:00"}]}
     * @response 404 {'msg':'not found','code':404,'data':'没有查到任何数据'}
     */
    public function index()
    {
        $data = Banci::get();
        if(count($data) == 0){
            return response()->json(encodeMsg("没有查到任何数据",404));
        }
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增班次
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     *
     * @bodyParam name string 班次名称
     * @bodyParam number int 上下班次数
     * @bodyParam work_time string 工作时间
     * @bodyParam clock boolean 是否打卡 0:打卡 1:不打卡
     * @bodyParam allowedLate int 允许迟到时间
     * @bodyParam seriousLate int 严重迟到时间
     * @bodyParam absenteeism int 旷工迟到时间
     *
     * @response 201 {"msg":"created","code":201,"data":{"name":"\u4e2d\u73ed","work_time":"10:00-14:00","id":3}}
     * @response 400 {"msg":"param error","code":400,"data":"\u53c2\u6570\u9519\u8bef"}
     */
    public function store(\App\Http\Requests\System\Banci $request)
    {
        $req = $request->input();
        if(count($req) == 0){
            return response()->json(encodeMsg("参数错误",400));
        }
        try{
            $res = Banci::saveData($req);
            return response()->json(encodeMsg($res,201));
        }catch (\Exception $e){
            dd($e->getMessage());
            return response()->json(encodeMsg("添加失败",400));
        }
    }

    /**
     * 查看班次
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @bodyParam id int required the banci id.
     *
     * @queryParam name 班次名称
     * @queryParam number 上下班次数
     * @queryParam work_time 工作时间
     * @queryParam clock 是否打卡 0:打卡 1:不打卡
     * @queryParam allowedLate 允许迟到时间
     * @queryParam seriousLate 严重迟到时间
     * @queryParam absenteeism 旷工迟到时间
     *
     * @response 200 {"msg":"success","code":200,"data":{"id":1,"name":"0\u73ed","work_time":"0:00-6:00"}}
     * @response 404 {"msg":"api not found","code":404,"data":{"message":"\u6ca1\u6709\u67e5\u5230\u4efb\u4f55\u6570\u636e"}}
     */
    public function show($id)
    {
        $data = Banci::find($id);
        if(is_null($data)){
            return response()->json(encodeMsg("没有查到任何数据",404));
        }
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 修改班次
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @bodyParam id int required 班次id.
     * @bodyParam name string 班次名称
     * @bodyParam number int 上下班次数
     * @bodyParam work_time string 工作时间
     * @bodyParam clock boolean 是否打卡 0:打卡 1:不打卡
     * @bodyParam allowedLate int 允许迟到时间
     * @bodyParam seriousLate int 严重迟到时间
     * @bodyParam absenteeism int 旷工迟到时间
     *
     * @queryParam name 班次名称
     * @queryParam number 上下班次数
     * @queryParam work_time 工作时间
     * @queryParam clock 是否打卡 0:打卡 1:不打卡
     * @queryParam allowedLate 允许迟到时间
     * @queryParam seriousLate 严重迟到时间
     * @queryParam absenteeism 旷工迟到时间
     *
     * @response 205 {"msg":"reset content","code":205,"data":{"message":"\u4fee\u6539\u6210\u529f"}}
     * @response 400 {"msg":"param error","code":400,"data":{"message":"\u53c2\u6570\u9519\u8bef"}}
     * @response 404 {"msg":"api not found","code":404,"data":{"message":"\u8bf7\u6c42\u4e0d\u5b58\u5728"}}
     */
    public function update(\App\Http\Requests\System\Banci $request, $id)
    {
        $banci = new Banci();
        if(is_null($banci->find($id))){
            return response()->json(encodeMsg("请求不存在",404));
        }
        $data = $request->input();
        if(count($data) ==0){
            return response()->json(encodeMsg("参数错误",400));
        }
        try{
            $banci->where(Fields::ID,$id)->update($data);
            return response()->json(encodeMsg('修改成功',205));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("修改失败",400));
        }
    }

    /**
     * 单条删除班次
     *
     * @param  int  $id 班次id
     * @return \Illuminate\Http\Response
     * @bodyParam id int required 班次id
     * @response 204 {'msg':"delete","code":"204","data":"删除成功"}
     * @response 400 {'msg':"param error","code":"400","data":"删除失败"}
     * @response 404 {'msg':"not found","code":"404","data":"请求不存在"}
     */
    public function destroy($id)
    {
        $banci = new Banci();
        $del = $banci->find($id);
        if(is_null($del)){
            return response()->json(encodeMsg("请求不存在",404));
        }
        try{
            $del->delete();
            return response()->json(encodeMsg("删除成功",204));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("删除失败",400));
        }
    }

}
